package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.domain.Comments;
import com.example.blog.domain.Response;
import com.example.blog.domain.UserInformation;
import com.example.blog.domain.Writings;
import com.example.blog.dto.comments.AdminSelectDto;
import com.example.blog.note.LogInfo;
import com.example.blog.note.NoToken;
import com.example.blog.note.Permissions;
import com.example.blog.service.impl.CommentsServiceImpl;
import com.example.blog.service.impl.UserInformationServiceImpl;
import com.example.blog.service.impl.WritingsServiceImpl;
import com.example.blog.utils.JwtUtils;
import com.example.blog.dto.comments.CommentsAddDto;
import com.example.blog.dto.comments.CommentsDto;
import com.example.blog.vo.comments.TemporaryContainers;
import io.jsonwebtoken.Claims;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@RestController
@Transactional
@Validated
@RequestMapping("/comments")
public class CommentsController extends BaseController{
    private final CommentsServiceImpl commentsService;
    @Autowired
    public CommentsController(CommentsServiceImpl commentsService){
       this.commentsService = commentsService;
    }

    //分页查询文章评论
    @NoToken
    @LogInfo("分页查询文章评论")
    @PostMapping("/articleComments")
    public Response articleComments(@RequestBody CommentsDto comments) {
        IPage page = new Page(comments.getIndex(), 10);
        LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comments::getWid, comments.getWid()).orderByDesc(Comments::getDateTime).eq(Comments::getStatue, 1);
        List<Object> list = new ArrayList<>();
        list.add(commentsService.page(page, wrapper).getRecords());
        list.add(page.getPages()); //多少页
        return new Response(Response.SUCCEED_CODE, "分页查询文章评论", list);
    }

    //添加评论
    @LogInfo("添加评论")
    @PostMapping("/addComments")
    public Response addComments(@RequestBody CommentsAddDto comments) {
        HttpServletRequest request = getHttpInfo();
        Claims claims = JwtUtils.checkToken(request.getHeader("token"));
        commentsService.save(
                new Comments(
                        comments.getWid(),
                        comments.getContent(),
                        Long.parseLong(claims.get("uid").toString()),
                        comments.getReplyCid(),
                        comments.getReplyUid(),
                        comments.getPlatform()));
        return new Response(Response.SUCCEED_CODE, "添加评论");
    }

    //评论删除
    @LogInfo("评论删除")
    @DeleteMapping("/del/{cid}")
    public Response delComments(@PathVariable("cid") long cid) {
        Comments comments = commentsService.getOne(new LambdaQueryWrapper<Comments>().eq(Comments::getCid,cid));
        if(comments.getUid() == getLogSign().getUid() || comments.getReplyUid() == getLogSign().getUid()){
            UpdateWrapper<Comments> wrapper = new UpdateWrapper<>();
            wrapper.eq("cid", cid).set("statue", 0);
            return new Response(Response.SUCCEED_CODE, "评论删除", commentsService.update(null, wrapper));
        }
        return new Response(Response.ERROR_CODE, "评论删除");
    }

    //评论删除
    @LogInfo("评论单个删除")
    @Permissions("/comments/admin/del")
    @DeleteMapping("/admin/del/{cid}")
    public Response delAdminComments(@PathVariable("cid") long cid) {
        LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comments::getCid,cid);
        boolean update = commentsService.remove( wrapper);
        if(update){
            return new Response(Response.SUCCEED_CODE, "删除成功", null);
        }
        return new Response(Response.ERROR_CODE, "删除失败");
    }

    //评论删除
    @LogInfo("批量评论删除")
    @Permissions("/comments/admin/delAll")
    @PostMapping("/admin/delAll")
    public Response delAdminAllComments(@RequestBody Long[] id) {
           UpdateWrapper<Comments> wrapper = new UpdateWrapper<>();
            wrapper.in("cid", id);
        boolean update = commentsService.remove( wrapper);
        if(update){
            return new Response(Response.SUCCEED_CODE, "删除成功", null);
        }
        return new Response(Response.ERROR_CODE, "删除失败");
    }


    //统计用户的总评论
    @NoToken
    @LogInfo("统计用户的总评论")
    @GetMapping("/commentsGetAll/{id}")
    public Response commentsGetAll(@PathVariable("id") long id) {
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", id).eq("statue", 1);
        return new Response(Response.SUCCEED_CODE, "统计用户的总评论", commentsService.count(wrapper));
    }

    //统计文章的总评论
    @NoToken
    @LogInfo("统计文章的总评论")
    @GetMapping("/commentsWritingsGetAll/{id}")
    public Response commentsWritingsGetAll(@PathVariable("id") long id) {
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("wid", id).eq("statue", 1);
        return new Response(Response.SUCCEED_CODE, "统计文章的总评论", commentsService.count(wrapper));
    }

    //Admin查询文章评论
    @LogInfo("Admin查询文章评论")
    @Permissions("/comments/admin/select")
    @PostMapping("/admin/select")
    public Response getAdminComments(@RequestBody AdminSelectDto comments) {
        return new Response(Response.SUCCEED_CODE, "分页查询文章评论",commentsService.AdminSelectComment( comments));
    }
}

